package BinaryTree;

import java.util.*;

public class BinaryTree {

    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }


    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) return;

        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);

    }

    //二叉树的前序遍历非递归
    public void preOrderNor(TreeNode root) {
        if(root == null) return;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            //cur == null
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

//    前序遍历带返回值
//    public List<Integer> preorderTraversal(TreeNode root) {
//        List<Integer> ret = new ArrayList<>();
//        if(root == null) return ret;
//
//        ret.add(root.val);
//        List<Integer> leftTree = preorderTraversal(root.left);
//        ret.addAll(leftTree);
//        List<Integer> rightTree = preorderTraversal(root.right);
//        ret.addAll(rightTree);
//        return ret;
//}

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) return;

        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);

    }



//    public List<Integer> inorderTraversal(TreeNode root) {
//        List<Integer> ret = new ArrayList<>();
//        if(root == null) return ret;
//
//
//        List<Integer> leftTree = inorderTraversal(root.left);
//        ret.addAll(leftTree);
//        ret.add(root.val);
//        List<Integer> rightTree = inorderTraversal(root.right);
//        ret.addAll(rightTree);
//        return ret;
//
//    }



    //二叉树的中序遍历非递归
    public void inOrderNor(TreeNode root) {
        if(root == null) return;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //cur == null
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) return;

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");

    }


    //二叉树的后序遍历非递归
    public void postOrderNor(TreeNode root) {
        if(root == null) return;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;

        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //cur == null
            TreeNode top = stack.peek();

            if(top.right == null || top.right == prev) {
                System.out.print(top.val+ " ");
                prev = top;
                stack.pop();
            }else {
                cur = top.right;
            }
        }
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) {
            queue.offer(root);
        }

        while(!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val+ " ");
            if(top.left != null) {
                queue.offer(top.left);
            }
            if(top.right != null) {
                queue.offer(top.right);
            }
        }
    }

    //层序遍历2：输入：root = [3,9,20,null,null,15,7]
    //         输出：       [[3],[9,20],[15,7]]
    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> res = new ArrayList<>();
        if(root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while(size != 0) {
                TreeNode top = queue.poll();
                list.add(top.val);
                if(top.left != null) {
                    queue.offer(top.left);
                }
                if(top.right != null) {
                    queue.offer(top.right);
                }
                size--;
            }
            res.add(list);
        }
        return res;
    }

    //判断一棵树是否为完全二叉树
    public boolean isCompleteTree (TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null) {
            queue.offer(root);
        }

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                return false;
            }
        }
        return true;
    }


    //从前序与中序遍历序列构造二叉树
//    public int preIndex = 0;
//    public TreeNode buildTree(int[] preorder, int[] inorder) {
//        buildTreeChild(preorder,inorder,0,inorder.length - 1);
//    }
//
//    private TreeNode buildTreeChild(int[] preorder,int[] inorder,int inbegin,int inend) {
//        //递归的出口，不满足这个条件，要么就是没有了左树，要么就是没有了右树
//        if(inbegin > inend) {
//            return null;
//        }
//
//        //先创建根节点
//        TreeNode root = new TreeNode(preorder[preIndex]);
//        //找到在中序遍历的位置
//        int rootIndex = findIndex(inorder,inbegin,inend,preorder[preIndex]);
//        preIndex++;
//
//        //先创建左树，再创建右树  本身是在前序遍历
//        root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex-1);
//        root.right = buildTreeChild(preorder,inorder,rootIndex+1,inend);
//
//        return root;
//    }
//
//    private int findIndex(int[] inorder,int inbegin,int inend,int key) {
//        for(int i = inbegin; i <= inend; i++) {
//            if(inorder[i] == key) {
//                return i;
//            }
//        }
//        return -1;
//    }


//    public List<Integer> postorderTraversal(TreeNode root) {
//        List<Integer> ret = new ArrayList<>();
//        if(root == null) return ret;
//
//
//        List<Integer> leftTree = postorderTraversal(root.left);
//        ret.addAll(leftTree);
//
//        List<Integer> rightTree = postorderTraversal(root.right);
//        ret.addAll(rightTree);
//        ret.add(root.val);
//        return ret;
//    }

    //获取树中结点的个数
    // 方法1：遍历过程中只要root不为空就++
    public int usedSize;
    public int size1(TreeNode root) {
        if(root == null) return 0;

        usedSize++;
        size1(root.left);
        size1(root.right);
        return usedSize;
    }


    //获取树中结点的个数
    // 方法2：左子树结点数 + 右子树结点数 + 1 = 整颗树的结点个数
    public int size2(TreeNode root) {
        if(root == null) return 0;

        return size2(root.left) + size2(root.right) + 1;
    }

    /**
     *     获取叶子结点的个数方法1：
     *     1.当root的左边和root的右边都为空的时候，那么就是叶子，则+1
     *     2.左树的叶子+右树的叶子 = 整棵树的叶子
     */
    public static int leafSize = 0;
    public int getLeafNodeCount(TreeNode root) {
        if(root == null) return 0;

        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;

    }

    //获取叶子结点的个数,方法2：
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) return 0;

        if(root.left == null && root.right == null) {
            return 1;
        }

        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }


    //获取树中第k层的结点个数
    public int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) return 0;

        if(k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    //整颗树的高度 = max(左树，右树) + 1
    public int getHeight(TreeNode root) {
        if(root == null) return 0;

        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);

        return (leftH > rightH ? leftH : rightH) + 1;
    }

    //查找val是否存在
    public TreeNode find(TreeNode root, char val) {
        if(root == null ) return null;
        if(root.val == val) {
            return root;
        }
        TreeNode leftL = find(root.left,val);
        if(leftL != null) {
            return leftL;
        }
        TreeNode rightR = find(root.right,val);
        if(rightR != null) {
            return rightR;
        }
        return null;
    }

}
